using System;
using System.Globalization;
using System.Windows.Forms;
using System.IO;

namespace PhotoScreenSaver
{
#if WINDOWS || XBOX
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        public static bool preview = false;
        public static IntPtr hwnd;
        public static string Path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
        static Form mform = new Form();
        [STAThread]
        static void Main(string[] args)
        {
            Args = args;
            if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\xconfig.acs"))
            {
                Stream fstream = File.Open(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\xconfig.acs",FileMode.Open);
                BinaryReader mreader = new BinaryReader(fstream);
                Path = mreader.ReadString();
                fstream.Close();
            }
            if (args.Length > 0)
            {
                string mode = args[0].ToLower(CultureInfo.InvariantCulture);

                if (mode.StartsWith("/c"))
                {
                    //Show configuration screen
                    
                    mform.Shown += new EventHandler(mform_Shown);
                    Application.Run(mform);
                    return;
                }
                else if (mode.StartsWith("/p"))
                {
                 //Render preview screen
                    try
                    {
                        hwnd = new IntPtr(long.Parse(args[1]));
                        preview = true;
                    }
                    catch (Exception er)
                    {
                        MessageBox.Show(er.Message);
                    }
                    return;
                }
            }

            using (Game1 game = new Game1())
            {
                game.Run();
            }
        }

        static void mform_Shown(object sender, EventArgs e)
        {
            FolderBrowserDialog mydlg = new FolderBrowserDialog();
            if (mydlg.ShowDialog() == DialogResult.OK)
            {
                Stream mstream = File.Open(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\xconfig.acs", FileMode.OpenOrCreate);
                mstream.SetLength(0);
                BinaryWriter mywriter = new BinaryWriter(mstream);
                mywriter.Write(mydlg.SelectedPath);
                mywriter.Flush();
                Path = mydlg.SelectedPath;
                mstream.Close();
            }
            
            
        }
        static string[] Args;
       
       
    }
    class nativeWindow : IWin32Window
    {

        #region IWin32Window Members
        IntPtr hwnd;
        public nativeWindow(IntPtr pointer)
        {
            hwnd = pointer;
        }
        public IntPtr Handle
        {
            get { return hwnd; }
        }

        #endregion
    }
#endif
}

